package com.hunliji.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hunliji.common.response.PageData;
import com.hunliji.monitor.dao.mapper.mt.UserInfoDAO;
import com.hunliji.monitor.dao.model.dataobject.UserInfoDO;
import com.hunliji.monitor.service.api.UserInfoService;
import com.hunliji.monitor.service.api.model.bo.UserInfoBO;
import com.hunliji.monitor.service.api.model.param.QueryUserParam;
import com.hunliji.monitor.share.component.ComponentLocator;
import com.hunliji.monitor.share.component.convertor.ConverterFacade;
import com.hunliji.monitor.share.utils.PageDataUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * File：UserInfoServiceImpl.java<br> Title: <br> Description: <br>
 *
 * @author lejin Date: 2022/10/10
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    UserInfoDAO userInfoDAO;

    @Resource
    ConverterFacade converterFacade;

    @Override
    public UserInfoBO getById(Long id) {
        if(Objects.isNull(id)){
            return null;
        }
        UserInfoDO userInfoDO = userInfoDAO.selectById(id);
        return converterFacade.convert(userInfoDO, UserInfoBO.class);
    }

    @Override
    public List<UserInfoBO> getListByQuery(String name, Long id) {
        LambdaQueryWrapper<UserInfoDO> queryWrapper = new LambdaQueryWrapper<UserInfoDO>()
            .eq(Objects.nonNull(id), UserInfoDO::getId, id)
            .eq(!StringUtils.isEmpty(name), UserInfoDO::getName, name)
            .eq(UserInfoDO::getDeleted, Boolean.FALSE);
        List<UserInfoDO> userInfoDOs = userInfoDAO.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(userInfoDOs)){
            return Lists.newArrayList();
        }
        return converterFacade.convertList(userInfoDOs, UserInfoBO.class);
    }

    @Override
    public List<UserInfoBO> getListByIds(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<UserInfoDO> queryWrapper = new LambdaQueryWrapper<UserInfoDO>()
            .in(UserInfoDO::getId, ids)
            .eq(UserInfoDO::getDeleted, Boolean.FALSE);
        List<UserInfoDO> userInfoDOs = userInfoDAO.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(userInfoDOs)){
            return Lists.newArrayList();
        }
        return converterFacade.convertList(userInfoDOs, UserInfoBO.class);
    }

    @Override
    public Map<Long, UserInfoBO> getByIds(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return Maps.newHashMap();
        }
        LambdaQueryWrapper<UserInfoDO> queryWrapper = new LambdaQueryWrapper<UserInfoDO>()
            .in(UserInfoDO::getId, ids)
            .eq(UserInfoDO::getDeleted, Boolean.FALSE);
        List<UserInfoDO> userInfoDOs = userInfoDAO.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(userInfoDOs)){
            return Maps.newHashMap();
        }
        return converterFacade.convertList(userInfoDOs, UserInfoBO.class).stream().collect(
            Collectors.toMap(UserInfoBO::getId, x -> x));
    }

    @Override
    public void insertOne(UserInfoBO userInfoBO) {
        if(Objects.isNull(userInfoBO)){
            return;
        }
        UserInfoDO userInfoDO = converterFacade.convert(userInfoBO, UserInfoDO.class);
        userInfoDAO.insert(userInfoDO);
        userInfoDO.setId(userInfoDO.getId());
    }

    @Override
    public void updateOne(UserInfoBO userInfoBO) {
        if(Objects.isNull(userInfoBO)){
            return;
        }
        UserInfoDO userInfoDO = converterFacade.convert(userInfoBO, UserInfoDO.class);
        userInfoDAO.updateById(userInfoDO);
    }

    @Override
    public void deleteOne(Long id) {
        if(Objects.isNull(id)){
            return;
        }
        LambdaUpdateWrapper<UserInfoDO> updateWrapper = new LambdaUpdateWrapper<UserInfoDO>()
            .eq(UserInfoDO::getId, id)
            .set(UserInfoDO::getDeleted, Boolean.TRUE);
        userInfoDAO.update(null, updateWrapper);
    }

    @Override
    public PageData<UserInfoBO> queryPage(QueryUserParam queryUserParam) {
        LambdaQueryWrapper<UserInfoDO> queryWrapper = new LambdaQueryWrapper<UserInfoDO>();
        if(StringUtils.isNotBlank(queryUserParam.getMatchName())){
            queryWrapper.like(UserInfoDO::getName,queryUserParam.getMatchName());
        }
        if(Objects.isNull(queryUserParam.getPageNum())){
            queryUserParam.setPageNum(1);
        }
        if(Objects.isNull(queryUserParam.getPageSize())){
            queryUserParam.setPageSize(100);
        }
        PageHelper.startPage(queryUserParam.getPageNum(),queryUserParam.getPageSize());
        List<UserInfoDO> userInfoDOList = userInfoDAO.selectList(queryWrapper);
        PageInfo<UserInfoDO> pageInfo = new PageInfo<>(userInfoDOList);
        if(CollectionUtils.isEmpty(userInfoDOList)){
            return PageDataUtil.getEmptyData();
        }
        List<UserInfoBO> userInfoBOList = ComponentLocator.getConverter().convertList(userInfoDOList,UserInfoBO.class);
        return PageDataUtil.getPageData((int)pageInfo.getTotal(),userInfoBOList);
    }

    @Override
    public UserInfoBO queryByMail(String mail) {
        LambdaQueryWrapper<UserInfoDO> queryWrapper = new LambdaQueryWrapper<UserInfoDO>();
        queryWrapper.eq(UserInfoDO::getDeleted,Boolean.FALSE);
        if(StringUtils.isNotBlank(mail)){
            queryWrapper.eq(UserInfoDO::getMail,mail);
        }
        UserInfoDO userInfoDO = userInfoDAO.selectOne(queryWrapper);
        if(Objects.isNull(userInfoDO)){
            return null;
        }
        return ComponentLocator.getConverter().convert(userInfoDO,UserInfoBO.class);
    }
}
